package pl.put.augrelshooter.logic.game;

import pl.put.augrelshooter.logic.detection.IDetectable;
import pl.put.augrelshooter.logic.game.weapon.Weapon;

public class Game {
	private IGameEventsListener eventsListener;

	private String name;
	private Player me;

	public Game(String name, Player me, IGameEventsListener eventsListener) {
		this.eventsListener = eventsListener;

		this.name = name;
		this.me = me;
	}

	public Player getPlayer() {
		return me;
	}

	/**
	 * Event informing, that player wants to fire his gun.
	 * 
	 * @return true, if fire was shot, false otherwise (there is no ammo).
	 */
	public void playerWantsToShot(IDetectable target) {
		if (me.getWeapon().fire()) {
			if (target != null) {
				switch (target.getType()) {
				case PLAYER:
					enemyWasHit();
					break;
				case AMMO:
					playerFoundAmmo();
					break;
				case MEDKIT:
					playerFoundMedkit(10);
					break;
				}
			}

			eventsListener.ammoLevelChanged();
		}
	}

	public void playerWasHit(int damage) {
		me.dealDamage(damage);

		eventsListener.healthLevelChanged();

		if (me.getHealthPoints() <= 0) {
			// inform server, that I am dead.
			eventsListener.playerIsDead();
		}
	}

	public void playerFoundMedkit(int helthGained) {
		me.heal(helthGained);

		eventsListener.healthLevelChanged();
	}

	public void playerFoundWeapon(Weapon newWeapon) {
		me.equipWeapon(newWeapon);

		eventsListener.weaponChanged();
	}

	public void playerFoundAmmo() {
		me.getWeapon().addAmmo();

		eventsListener.ammoLevelChanged();
	}

	public String getGameName() {
		return name;
	}

	public void newPlayerJoined(Player player) {
		eventsListener.newPlayerJoined(player);
	}

	public void enemyWasHit() {
		String dmg = Integer.toString(me.getWeapon().getDamage());
		eventsListener.enemyHit(dmg);
	}

	public void previousWeapon() {
		me.changeWeaponPrev();
		eventsListener.weaponChanged();
	}

	public void nexWeapon() {
		me.changeWeaponNext();
		eventsListener.weaponChanged();
	}
}
